package com.gadgets.framework.fm03.service.impl;

import com.gadgets.framework.common.constant.GadgetsExceptionConstant;
import com.gadgets.framework.common.dto.Pagination;
import com.gadgets.framework.common.exception.GadgetsDataNumException;
import com.gadgets.framework.common.exception.GadgetsException;
import com.gadgets.framework.common.exception.GadgetsServiceException;
import com.gadgets.framework.common.util.PaginationUtil;
import com.gadgets.framework.fm03.dto.BusiFlowOperationDTO;
import com.gadgets.framework.fm03.dto.BusiFlowQueryDTO;
import com.gadgets.framework.fm03.pojo.BusiFlowInfo;
import com.gadgets.framework.fm03.repository.IBusiFlowInfoRepository;
import com.gadgets.framework.fm03.service.IBusiFlowManagerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author UncleXiu
 * @version V1.0.1
 * @data 2021-01-28
 * @moduel gadgets
 *
 */
@Service
@Transactional(transactionManager = "gadgetsDbTransactionManage", rollbackFor = Throwable.class)
public class BusiFlowManagerServiceImpl implements IBusiFlowManagerService {

    @Autowired
    private IBusiFlowInfoRepository busiFlowInfoRepository;

    /**
     * 新增流程信息
     *
     * @param flowInfo 流程信息
     *
     * @throws GadgetsServiceException 异常信息
     */
    @Transactional(transactionManager = "gadgetsDbTransactionManage", rollbackFor = Throwable.class)
    @Override
    public void addBusiFlowInfo(BusiFlowInfo flowInfo) throws GadgetsException {
        int i = busiFlowInfoRepository.insertFlowInfo(flowInfo);
        if(i!=1){
            throw new GadgetsDataNumException(1,i);
        }
    }

    /**
     * 分页查询业务流程信息
     *
     * @param queryDTO   查询条件
     * @param pagination 分页条件
     *
     * @return 查询结果
     *
     * @throws GadgetsException 异常信息
     */
    @Transactional(transactionManager = "gadgetsDbTransactionManage", rollbackFor = {Throwable.class})
    @Override
    public List<BusiFlowInfo> queryBusiFlowByCondition(BusiFlowQueryDTO queryDTO, Pagination pagination) throws GadgetsException {
        PaginationUtil.bulidPaginationInfo(pagination);
        queryDTO.addDesc("ID");
        List<BusiFlowInfo> result = busiFlowInfoRepository.queryBusiFlowByCondition(queryDTO);
        PaginationUtil.getPaginationResultInfo(result,pagination);
        return result;
    }

    /**
     * 更新业务流程状态
     * @param operationDTO              变更方法
     * @throws GadgetsServiceException  异常信息
     */
    @Transactional(transactionManager = "gadgetsDbTransactionManage", rollbackFor = Throwable.class)
    @Override
    public void updateBusiFlowStatus(BusiFlowOperationDTO operationDTO) throws GadgetsServiceException {
        int i = busiFlowInfoRepository.updateFlowStatus(operationDTO);
        if(1!=i){
            throw new GadgetsServiceException(GadgetsExceptionConstant.DATA_OPER_NUM_ERROR,1,i);
        }
    }

}
